package com.yjyw.tiexie.widget.tab;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.yjyw.tiexie.R;

import java.util.ArrayList;
import java.util.List;

public class HomeTab extends XTabLayout {

    private int normalColor;
    private int checkColor;
    private Drawable normalBg;
    private Drawable checkBg;
    private Context context;
    private List<View> msgTabs;
    private List<String> pointTags = new ArrayList<>();
    private int checkPosition = 0;
    private String tag;
    private int typeIndex;
    private List<Children> tabs;

    public HomeTab(Context context) {
        this(context, null, 0);
    }

    public HomeTab(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public HomeTab(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.HomeTab);
        normalColor =
                ta.getColor(R.styleable.HomeTab_tab_noraml_color, ContextCompat.getColor(context, R.color.text_999999));
        checkColor =
                ta.getColor(R.styleable.HomeTab_tab_check_color, ContextCompat.getColor(context, R.color.black));
        normalBg = ta.getDrawable(R.styleable.HomeTab_tab_normal_bg);
        checkBg = ta.getDrawable(R.styleable.HomeTab_tab_check_bg);
        ta.recycle();
        this.context = context;
    }

    public void setNormalTabs(String tag, int typeIndex, List<Children> tabs) {
        this.tag = tag;
        this.typeIndex = typeIndex;
        this.tabs = tabs;
        removeAllTabs();
        msgTabs = new ArrayList<>();
        pointTags = new ArrayList<>();
        LayoutInflater inflater = LayoutInflater.from(context);
        int tabCount = tabs.size();
        int defIndex = 0;

        for (int index = 0; index < tabCount; index++) {
            Children tabItem = tabs.get(index);
            View msgTab = LayoutInflater.from(context).inflate(R.layout.item_msg_tab, null);
            TextView title = msgTab.findViewById(R.id.title);
            LinearLayout liner = msgTab.findViewById(R.id.liner);
            msgTabs.add(msgTab);
            title.setTextColor(index == defIndex ? checkColor : normalColor);
            if (normalBg != null && checkBg != null) {
                liner.setBackground(index == defIndex ? checkBg : normalBg);
            }
            title.setEnabled(false);
//            title.setTextSize(TypedValue.COMPLEX_UNIT_SP, tag.equals("child") ? 14 : 16);
            title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);

            title.setText(tabItem.getName());
            Tab tab = newTab().setCustomView(msgTab);
            addTab(tab);
        }
    }


    public void setTabSelected(final TabSelected tabListener) {
        addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(Tab tab) {
                if (msgTabs == null || tab == null) {
                    return;
                }
                TextView title = msgTabs.get(tab.getPosition()).findViewById(R.id.title);
                LinearLayout liner = msgTabs.get(tab.getPosition()).findViewById(R.id.liner);
                title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
                checkPosition = tab.getPosition();
                title.setTextColor(checkColor);
                if (checkBg != null) {
                    liner.setBackground(checkBg);
                }
                if (tabListener != null) {
                    tabListener.onTabSelected(tab.getPosition());
                }
            }

            @Override
            public void onTabUnselected(Tab tab) {
                if (msgTabs == null || tab == null) {
                    return;
                }
                TextView title = msgTabs.get(tab.getPosition()).findViewById(R.id.title);
                LinearLayout liner = msgTabs.get(tab.getPosition()).findViewById(R.id.liner);
                title.setTextSize(TypedValue.COMPLEX_UNIT_SP, 17);
                title.setTextColor(normalColor);
                if (normalBg != null) {
                    liner.setBackground(normalBg);
                }
            }

            @Override
            public void onTabReselected(Tab tab) {
            }
        });
    }

    public int getCheckPosition() {
        return checkPosition;
    }

}
